define(["dojo/_base/declare", "esri/layers/TiledMapServiceLayer", "esri/geometry/Extent", "esri/SpatialReference", "esri/layers/TileInfo"],
    function (declare, TiledMapServiceLayer, Extent, SpatialReference, TileInfo) {
        return declare(TiledMapServiceLayer, {
            baiduAK: "1H8Dhi2pGmOMYbN4EcaAGr1rv8f7Gmjz", //百度开发密钥
            // 构造函数
            constructor: function (properties) {
                this.spatialReference = new SpatialReference({
                    wkid: 102100  //webMercator投影
                });
                declare.safeMixin(this, properties);
                // 图层提供的起始显示范围以及整个图层的地理范围
                // this.fullExtent = new Extent(-16777360, -16797630, 16777360, 16797630, this.spatialReference);
                this.fullExtent = new Extent(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787, this.spatialReference);
                this.initialExtent = new Extent(5916776.8, 1877209.3, 19242502.6, 7620381.8, this.spatialReference);
                // 图层提供的切片信息
                this.tileInfo = new TileInfo({
                    "rows": 256,
                    "cols": 256,
                    "compressionQuality": 0,
                    "origin": {
                        "x": -16778585,
                        "y": 16799140
                    },
                    "spatialReference": {
                        "wkid": 102100
                    },
                    "lods": [{
                        "level": 0,
                        "resolution": 131072,
                        "scale": 131072 * 256
                    },
                        {
                            "level": 1,
                            "resolution": 65536,
                            "scale": 65536 * 256
                        },
                        {
                            "level": 2,
                            "resolution": 32768,
                            "scale": 32768 * 256
                        },
                        {
                            "level": 3,
                            "resolution": 16384,
                            "scale": 16384 * 256
                        },
                        {
                            "level": 4,
                            "resolution": 8192,
                            "scale": 8192 * 256
                        },
                        {
                            "level": 5,
                            "resolution": 4096,
                            "scale": 4096 * 256
                        },
                        {
                            "level": 6,
                            "resolution": 2048,
                            "scale": 2048 * 256
                        },
                        {
                            "level": 7,
                            "resolution": 1024,
                            "scale": 1024 * 256
                        },
                        {
                            "level": 8,
                            "resolution": 512,
                            "scale": 512 * 256
                        },
                        {
                            "level": 9,
                            "resolution": 256,
                            "scale": 256 * 256
                        },
                        {
                            "level": 10,
                            "resolution": 128,
                            "scale": 128 * 256
                        },
                        {
                            "level": 11,
                            "resolution": 64,
                            "scale": 64 * 256
                        },
                        {
                            "level": 12,
                            "resolution": 32,
                            "scale": 32 * 256
                        },
                        {
                            "level": 13,
                            "resolution": 16,
                            "scale": 16 * 256
                        },
                        {
                            "level": 14,
                            "resolution": 8,
                            "scale": 8 * 256
                        },
                        {
                            "level": 15,
                            "resolution": 4,
                            "scale": 4 * 256
                        },
                        {
                            "level": 16,
                            "resolution": 2,
                            "scale": 2 * 256
                        },
                        {
                            "level": 17,
                            "resolution": 1,
                            "scale": 1 * 256
                        },
                        {
                            "level": 18,
                            "resolution": 0.5,
                            "scale": 0.5 * 256
                        },
                        {
                            "level": 19,
                            "resolution": 0.25,
                            "scale": 0.25 * 256
                        }
                    ]
                });

                // 设置图层的loaded属性，并触发onLoad事件
                this.loaded = true;
                this.onLoad(this);
            },
            getTileUrl: function (level, row, col) {
                var zoom = level - 1;
                var offsetX = Math.pow(2, zoom);
                var offsetY = offsetX - 1;
                var numX = col - offsetX;
                var numY = (-row) + offsetY;
                zoom = level + 1;
                var url = "http://api1.map.bdimg.com/customimage/tile?&x=" + numX + "&y=" + numY + "&z=" + zoom + "&ak=" + this.baiduAK + "&styles=t%3Awater%7Ce%3Aall%7Cc%3A%23021019%2Ct%3Ahighway%7Ce%3Ag.f%7Cc%3A%23000000%2Ct%3Ahighway%7Ce%3Ag.s%7Cc%3A%23147a92%2Ct%3Aarterial%7Ce%3Ag.f%7Cc%3A%23000000%2Ct%3Aarterial%7Ce%3Ag.s%7Cc%3A%230b3d51%2Ct%3Alocal%7Ce%3Ag%7Cc%3A%23000000%2Ct%3Aland%7Ce%3Aall%7Cc%3A%2308304b%2Ct%3Arailway%7Ce%3Ag.f%7Cc%3A%23000000%2Ct%3Arailway%7Ce%3Ag.s%7Cc%3A%2308304b%2Ct%3Asubway%7Ce%3Ag%7Cl%3A-70%2Ct%3Abuilding%7Ce%3Ag.f%7Cc%3A%23000000%2Ct%3Aall%7Ce%3Al.t.f%7Cc%3A%23857f7f%2Ct%3Aall%7Ce%3Al.t.s%7Cc%3A%23000000%2Ct%3Abuilding%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Agreen%7Ce%3Ag%7Cc%3A%23062032%2Ct%3Aboundary%7Ce%3Aall%7Cc%3A%231e1c1c%2Ct%3Amanmade%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Apoi%7Ce%3Aall%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.i%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.t.f%7Cv%3Aon%7Cc%3A%232da0c6";
                return url;
            }
        });
    }
)
